package interfaceComUsuario;

import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import acessorios.Carta;
import acessorios.RolagemDeadlands;

public class DropTargetListner extends DropTargetAdapter
{
	@SuppressWarnings("unused")
	private DropTarget dropTarget;
	private LabelCarta labelCarta;
	private JTextField textField;
	private JLabel label;
	private JPanel panel;
	
	public DropTargetListner(LabelCarta labelCarta)
	{
		this(labelCarta, null, null, null);
	}
	
	public DropTargetListner(LabelCarta labelCarta, JTextField textField, JLabel label, JPanel panel)
	{
		this.labelCarta = labelCarta;
		this.textField = textField;
		this.label = label;
		this.panel = panel;
		dropTarget = new DropTarget(labelCarta, DnDConstants.ACTION_MOVE, this, true, null);
	}

	@Override
	public void drop(DropTargetDropEvent dtde)
	{
		try
		{
			Transferable tr = dtde.getTransferable();
			Carta carta = (Carta) tr.getTransferData(Carta.getFlavor());
			if(dtde.isDataFlavorSupported(Carta.getFlavor()))
			{
				dtde.acceptDrop(DnDConstants.ACTION_MOVE);
				String nomeDoAtributo = this.labelCarta.getText();
				this.labelCarta.setCarta(carta);
				if(this.textField != null)
				{
					this.textField.setText(carta.toString());
				}
				if(this.label != null)
				{
					this.label.setText(RolagemDeadlands.rolagemAssociada(carta));
				}
				if(this.panel != null)
				{
					Border borderLowered = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
					this.panel.setBorder(borderLowered);
					this.panel.setEnabled(false);
				}
				this.labelCarta.setText(nomeDoAtributo);
				dtde.dropComplete(true);
			}
			else
			{
				dtde.rejectDrop();
			}
		}
		catch (Exception e)
		{
//			e.printStackTrace();
			dtde.rejectDrop();
		}
	}

}
